Imports UniversalLayer.ULMain

Public Class ULConverter

    ''' <summary>
    ''' Boolean Style as other datatypes for conversion
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum eBooleanStyle
        ''' <summary>
        ''' Boolean words :: True/False
        ''' </summary>
        ''' <remarks></remarks>
        BoolText = -2
        ''' <summary>
        ''' Boolean character :: T/F
        ''' </summary>
        ''' <remarks></remarks>
        BoolChr = -1
        ''' <summary>
        ''' Integer :: 1/0
        ''' </summary>
        ''' <remarks></remarks>
        Int = 0
        ''' <summary>
        ''' Character :: Y/N
        ''' </summary>
        ''' <remarks></remarks>
        Chr = 1
        ''' <summary>
        ''' In words :: YES/NO
        ''' </summary>
        ''' <remarks></remarks>
        FullText = 2
    End Enum

#Region "SQL/XML :: Clean-up & Restore"

    ''' <summary>
    ''' Cleans SQL to remove any invalid characters
    ''' before storage in DB
    ''' </summary>
    ''' <param name="sString">SQL to clean</param>
    ''' <returns>Cleaned up SQL</returns>
    ''' <remarks></remarks>
    Public Shared Function CleanSQL(ByRef sString As String) As String
        If Not sString Is Nothing Then sString = sString.Replace("'", "''")
        Return sString
    End Function

    ''' <summary>
    ''' Restores SQL from DB which was cleaned earlier
    ''' after retrieving from DB
    ''' </summary>
    ''' <param name="sString">SQL to restore</param>
    ''' <returns>Restored SQL</returns>
    ''' <remarks></remarks>
    Public Shared Function RestoreSQL(ByRef sString As String) As String
        If Not sString Is Nothing Then sString = sString.Replace("''", "'")
        Return sString
    End Function

    ''' <summary>
    ''' Cleans XML to remove any invalid characters
    ''' before storage in XML
    ''' </summary>
    ''' <param name="sString">XML to clean</param>
    ''' <returns>Cleaned up XML</returns>
    ''' <remarks></remarks>
    Public Shared Function CleanXML(ByRef sString As String) As String
        If Not sString Is Nothing Then
            sString = sString.Replace("<", "&lt;")
            sString = sString.Replace(">", "&gt;")
            sString = sString.Replace("&", "&amp;")
            sString = sString.Replace("'", "&apos;")
            sString = sString.Replace("""", "&quot;")
        End If
        Return sString
    End Function

    ''' <summary>
    ''' Restores TAG from XML which was cleaned earlier
    ''' after retrieving from XML
    ''' </summary>
    ''' <param name="sString">XML to restore</param>
    ''' <returns>Restored XML</returns>
    ''' <remarks></remarks>
    Public Shared Function RestoreXML(ByRef sString As String) As String
        If Not sString Is Nothing Then
            sString = sString.Replace("&lt;", "<")
            sString = sString.Replace("&gt;", ">")
            sString = sString.Replace("&amp;", "&")
            sString = sString.Replace("&apos;", "'")
            sString = sString.Replace("&quot;", """")
        End If
        Return sString
    End Function

#End Region

#Region "Converters"
    '#Region "Format BOOLEAN/CSV"

    ''' <summary>
    ''' Converts Boolean Text to Boolean
    ''' </summary>
    ''' <param name="sBoolFriendly">Boolean Text</param>
    ''' <returns>Boolean value</returns>
    ''' <remarks></remarks>
    Public Shared Function ConvertStringToBool(ByVal sBoolFriendly As String) As Boolean
        Select Case sBoolFriendly.ToUpper
            Case "Y", "YES", "TRUE", "1", "TRUE", "T"
                Return True
            Case "N", "NO", "FALSE", "0", "FALSE", "F"
                Return False
            Case Else
                Return Nothing
        End Select
    End Function

    ''' <summary>
    ''' Converts Boolean to specified text format
    ''' </summary>
    ''' <param name="bValue">Boolean value</param>
    ''' <param name="eBooleanStyle">Boolean Text Format (default: Y/N)</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ConvertBoolToString(ByVal bValue As Boolean, Optional ByVal eBooleanStyle As eBooleanStyle = eBooleanStyle.Chr) As String
        Dim sBooleanText As String
        Select Case eBooleanStyle
            Case eBooleanStyle.BoolText
                If bValue Then sBooleanText = "True" Else sBooleanText = "False"
            Case eBooleanStyle.Int
                If bValue Then sBooleanText = "1" Else sBooleanText = "0"
            Case eBooleanStyle.Chr
                If bValue Then sBooleanText = "Y" Else sBooleanText = "N"
            Case eBooleanStyle.FullText
                If bValue Then sBooleanText = "Yes" Else sBooleanText = "No"
            Case Else
                sBooleanText = Nothing
        End Select

        Return sBooleanText
        sBooleanText = Nothing
    End Function

    ''' <summary>
    ''' Gets Boolean Text Format
    ''' </summary>
    ''' <param name="sBooleanText">Boolean Text</param>
    ''' <returns>Boolean Text Format</returns>
    ''' <remarks></remarks>
    Public Shared Function GetBooleanTextType(ByVal sBooleanText As String) As eBooleanStyle
        Select Case sBooleanText
            Case "True", "False"
                Return eBooleanStyle.BoolText
            Case "1", "0"
                Return eBooleanStyle.Int
            Case "Y", "N"
                Return eBooleanStyle.Chr
            Case "Yes", "No"
                Return eBooleanStyle.FullText
            Case Else
                Return Nothing
        End Select
    End Function

    ''' <summary>
    ''' Converts one boolean text format to another; 
    ''' Can also be used as a SUB by a CALL keyword
    ''' </summary>
    ''' <param name="sBooleanText">Boolean Text</param>
    ''' <param name="eBooleanStyle">Boolean Text Format</param>
    ''' <returns>Boolean Text converted to specified format</returns>
    ''' <remarks></remarks>
    Public Shared Function ConvertBoolText(ByRef sBooleanText As String, ByVal eBooleanStyle As eBooleanStyle) As String
        sBooleanText = ConvertBoolToString(ConvertStringToBool(sBooleanText), GetBooleanTextType(sBooleanText))
        Return sBooleanText
    End Function

    ''' <summary>
    ''' NO COMMENTS
    ''' </summary>
    ''' <param name="sBoolText"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ToggleBoolText(ByRef sBoolText As String) As String
        sBoolText = ConvertBoolToString(Not ConvertStringToBool(sBoolText), GetBooleanTextType(sBoolText))
        Return sBoolText
    End Function

#End Region

End Class
